//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var status1 = '0';
var isshiMing = '';
var issignqiyuesuoAgreement = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    userType: "",
    // 契约锁
    eventChannel: null,
    page: '',
    ticket: '',
    env: '',
    IDcard: '',
    realName: '',
    change: '',
    changeValue: "提交认证",
    flag: false,
    hidden: true,
    status: '0',
    disabled: false,
    opacity: "0.5",
    agreeAndReceipt: "0",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
    })
    this.GetUserInfoByIDs();
    // var showDialog = wx.getStorageSync('showDialog');
    // if (showDialog != '1') {
    // wx.showModal({
    //   title: '提示',
    //   content: '本软件尊重并保护所有使用服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务，本软件会按照本隐私权政策的规定使用和披露您的个人信息。但本软件将以高度的勤勉、审慎义务对待这些信息。除本隐私权政策另有规定外，在未征得您事先许可的情况下，本软件不会将这些信息对外披露或向第三方提供。本软件会不时更新本隐私权政策。您在同意本软件服务使用协议之时，即视为您已经同意本隐私权政策全部内容。本隐私权政策属于本软件服务使用协议不可分割的一部分!你可以在APP内，关于我们里面查看隐私政策及用户注册协议。',
    //   showCancel: false
    // })
    // wx.setStorageSync('showDialog', '1');
    // }
  },
  // 接受协议
  agreeAndReceipt: function (e) {
    if (e.detail.value.length == 0) {
      this.setData({
        agreeAndReceipt: "0",
        opacity: 0.5
      })
    } else {
      this.setData({
        agreeAndReceipt: "1",
        opacity: 1
      })
    }
  },

  // 跳转 易易部落用户服务协议
  navigate: function () {
    wx.navigateTo({
      url: '/packageA/yonghuxieyi',
    })
  },
  navigateTo: function () {
    wx.navigateTo({
      url: '/packageA/yinsizhengce',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  //获取用户信息
  GetUserInfoByIDs: function () {
    var that = this;
    var uid = this.data.uid;
    var IDcard = this.data.IDcard;
    var realName = this.data.realName;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByIDs&UID=" + uid),
      success: function (res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        isshiMing = json.IsShiMing;
        issignqiyuesuoAgreement = json.IsSignQiYueSuoAgreement;
        realName = json.TrueName;
        IDcard = json.IDNumber;
        console.log(json);
        that.setData({
          realName: realName,
          IDcard: IDcard,
        })
        if (status1 == '1') {
          that.setData({
            changeValue: '签署合同',
          })
        }
        if (isshiMing == '1') {
          that.setData({
            disabled: true
          })
          if (issignqiyuesuoAgreement == '1') {
            that.setData({
              changeValue: '查看合同',
            })
          } else {
            that.setData({
              changeValue: '签署合同',
            })
          }
        }
      }
    })
  },
  // 提交认证
  formSubmit: function (e) {
    var that = this;
    var status = that.data.status;
    var uid = this.data.uid;
    var ticket = this.data.ticket;
    var ticket1 = this.data.ticket1;
    var realName = '';
    var IDcard = '';
    var agreeAndReceipt = this.data.agreeAndReceipt;
    if (agreeAndReceipt == "0") {
      wx.showToast({
        title: "请同意并接受以上协议！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (issignqiyuesuoAgreement == '1') {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=GetQiYueSuoContractPage&UID=" + uid),
        success: function (res) {
          var urlString = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
          var model = [urlString];
          var model1 = JSON.stringify(model);
          wx.navigateTo({
            url: '/packageA/renzheng/renzheng?urlString=' + model1,
          })
          wx.setStorageSync('showString', urlString)
        }
      })
    } else {
      console.log("method=UpUserInfoByUID&UID=" + uid + "&UserTrueName=" + e.detail.value.realName + "&UserIdCard=" + e.detail.value.IDcard);
      // console.log(e);
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=UpUserInfoByUID&UID=" + uid + "&UserTrueName=" + e.detail.value.realName + "&UserIdCard=" + e.detail.value.IDcard),
        success: function (res) {
          if (JSON.parse(baseJs.decryUrl(res.data)).ResultFlag == 1) {
            console.log(JSON.parse(baseJs.decryUrl(res.data)));
            if (status1 == '0') {
              wx.request({
                url: baseJs.api + baseJs.encryUrl("method=GetUserAuthTicket&UID=" + uid),
                success: function (res) {
                  var json = JSON.parse(baseJs.decryUrl(res.data));
                  ticket = json.ResultData;
                  console.log(json);
                  // wx.setStorageSync('ticket', json.ResultData);
                  let eventChannel = null;
                  wx.navigateTo({
                    url: `plugin://qyssdk-plugin/personal-auth?ticket=` + ticket + `&env=com&hasCb=true`,
                    events: {
                      signSuccessCb: () => { // 签署成功回调
                        // console.log('signComplete');
                        // const url = '/pages/bbb/bbb'; // 需要跳转的小程序页面地址，必须是绝对路径，可不传
                        eventChannel.emit('jumpTo'); // 触发跳转逻辑，回调存在时必需调用，url不传默认返回
                      },
                      authSuccessCb() { // 认证成功回调
                        status1 = '1';
                        that.setData({
                          status1: '1',
                        })

                        wx.showModal({
                          title: '您已完成个人认证，请再次点击签署合同！',
                          icon: 'none',
                          duration: 2000
                        });
                        console.log(that.data.status)
                        eventChannel.emit('jumpTo', '/packageA/realnameauthentication/realnameauthentication');
                      },
                      authFailCb() { // 认证失败回调
                        console.log('authFail');
                        eventChannel.emit('jumpTo');
                      },
                    },
                    success(res) {
                      eventChannel = res.eventChannel;
                    },
                  })
                }
              })
            } else {
              wx.request({
                url: baseJs.api + baseJs.encryUrl("method=GetContractTicket&UID=" + uid),
                success: function (res) {
                  var json = JSON.parse(baseJs.decryUrl(res.data));
                  ticket1 = json.ResultData;
                  console.log(json);
                  wx.navigateTo({
                    url: `plugin://qyssdk-plugin/doc?ticket=` + ticket1 + `&env=com&hasCb=true`,
                    events: {
                      signSuccessCb: () => { // 签署成功回调
                        wx.showToast({
                          title: '签署成功',
                          icon: 'none',
                          duration: 2000
                        });
                      },

                    },
                  })

                }
              })
            }
          } else {
          }
        }

      })
    }
  },
  
})